package com.afkt.project.ui.slice;

import com.afkt.project.base.app.BaseActivity;
import com.afkt.project.model.bean.ItemStickyBean;
import com.afkt.project.ui.adapter.ItemStickyAdapter;
import com.ohos.afkt.ResourceTable;
import com.dev.utils.common.ChineseUtils;
import com.dev.utils.common.DateUtils;
import com.dev.utils.common.RandomUtils;
import ohos.agp.components.*;

import java.util.ArrayList;
import java.util.List;

public class ItemStickyActivity extends BaseActivity {

    /**
     * 标题栏
     */
    private DirectionalLayout bar_layout;
    private DirectionalLayout bar;
    private Image itemSticky_return;

    //listContainer
    private ListContainer listContainer;
    private List<String> titles = new ArrayList<>();

    @Override
    protected int baseLayoutId() {
        return ResourceTable.Layout_activity_itemsticky;
    }

    @Override
    protected void initView() {
        bar_layout = (DirectionalLayout) findComponentById(ResourceTable.Id_itemSticky_layout);
        bar = (DirectionalLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_itemsticky_bar, null, false);
        bar_layout.addComponent(bar);
        itemSticky_return = (Image) bar.findComponentById(ResourceTable.Id_itemSticky_return);

        listContainer = (ListContainer) findComponentById(ResourceTable.Id_itemSticky_listContainer);
       /* hearder_title = (Text) findComponentById(ResourceTable.Id_header_title);
        listContainer.addComponent(hearder_title);*/

       ItemStickyAdapter adapter = new ItemStickyAdapter(getList(),this);
       adapter.setTitles(titles);
        listContainer.setItemProvider(adapter);

    }

    private List<ItemStickyBean> getList() {
        List<ItemStickyBean> lists = new ArrayList<>();

        long time = System.currentTimeMillis();

        for (int i = 0; i < 8; i++) {
            lists.add(new ItemStickyBean(ChineseUtils.randomWord(RandomUtils.getRandom(3, 12)), time));
        }

        titles.add(newTitle(time));

        time -= DateUtils.DAY;
        for (int i = 0; i < 5; i++) {
            lists.add(new ItemStickyBean(ChineseUtils.randomWord(RandomUtils.getRandom(3, 12)), time));
        }

        titles.add(newTitle(time));

        time -= DateUtils.DAY * 3;
        for (int i = 0; i < 4; i++) {
            lists.add(new ItemStickyBean(ChineseUtils.randomWord(RandomUtils.getRandom(3, 12)), time));
        }

        titles.add(newTitle(time));

        time -= DateUtils.DAY * 2;
        for (int i = 0; i < 6; i++) {
            lists.add(new ItemStickyBean(ChineseUtils.randomWord(RandomUtils.getRandom(3, 12)), time));
        }

        time -= DateUtils.DAY;
        for (int i = 0; i < 7; i++) {
            lists.add(new ItemStickyBean(ChineseUtils.randomWord(RandomUtils.getRandom(3, 12)), time));
        }
        titles.add(newTitle(time));

        time -= DateUtils.DAY * 10;
        for (int i = 0; i < 7; i++) {
            lists.add(new ItemStickyBean(ChineseUtils.randomWord(RandomUtils.getRandom(3, 12)), time));
        }

        titles.add(newTitle(time));

        time -= DateUtils.DAY * 4;
        for (int i = 0; i < 10; i++) {
            lists.add(new ItemStickyBean(ChineseUtils.randomWord(RandomUtils.getRandom(3, 12)), time));
        }

        titles.add(newTitle(time));


        return lists;
    }

    private String newTitle(Long time) {

        String timeTile;

        String format = "yyyy.MM.dd";
        // 进行格式化
        String timeFormat = DateUtils.formatTime(time, format);
        // 获取当前时间
        String currentTime = DateUtils.getDateNow(format);
        // 设置标题
        if (currentTime.equals(timeFormat)) {
            timeTile = "今日";
        } else {
            timeTile = DateUtils.formatTime(time, DateUtils.MMdd2);
        }
        return timeTile;
    }


    @Override
    protected void initListener() {
        itemSticky_return.setClickedListener(component -> onBackPressed());
    }

    @Override
    protected void onBackground() {
        bar_layout.removeComponent(bar);
        super.onBackground();
    }
}
